import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import InitialPage from '../components/InitialPage.vue'
import Users from '../components/user/users.vue'
import Powers from '../components/Powers/powers.vue'
import Roles from '../components/Powers/roles.vue'



Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    { 
      path: '/home', 
      component: Home , 
      redirect: '/InitialPage',
      children: [
        {
          path: '/InitialPage', component: InitialPage
        },
        {
          path: '/Users', component: Users
        },
        {
          path: '/Powers', component: Powers
        },
        {
          path: '/Roles', component: Roles
        }
      ]
    }
  ]
})

// 非时时监听
router.beforeEach((to, from, next) => {
  if(to.path === '/login') return next();
  let tokenStr = window.sessionStorage.getItem('token');
  // console.log(tokenStr);
  if(!tokenStr) return next('/login');
  next()
})
export default router
